package com.my.thread;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

public class MyThreadUtil {


    private ScheduledExecutorService scheduledExecutorService;
    private static MyThreadUtil myThreadUtil = null;

    private MyThreadUtil() {

        ThreadFactory threadFactory = new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread("MyGatewayLogUtil_Thread");
                return t;
            }
        };

        scheduledExecutorService = Executors.newScheduledThreadPool(1);
        scheduledExecutorService.scheduleAtFixedRate(new MyThread(), 0, 10, TimeUnit.SECONDS);
    }


    public static MyThreadUtil getInstance(){
        if(myThreadUtil == null){
            myThreadUtil = new MyThreadUtil();
        }
        return myThreadUtil;
    }


    public void sendMsg(String msg){
        System.out.println("=======need to send msg is: " + msg);

        Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(20);
                    System.out.println(Thread.currentThread().getName() + " run time: "+System.currentTimeMillis());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        };
        scheduledExecutorService.execute(myRunnable);
    }





}
